//
//  UINib+BundleName.m
//  EpubSDK
//
//  Created by linziyuan on 16/12/5.
//  Copyright © 2016年 creative. All rights reserved.
//

#import "UINib+BundleName.h"
#import <objc/runtime.h>

@implementation UINib (BundleName)

+ (void)load {
    Method fromMethod = class_getClassMethod([self class], @selector(nibWithNibName:bundle:));
    Method toMethod = class_getClassMethod([self class], @selector(jrsdk_bundleNibWithNibName:bundle:));
    
    method_exchangeImplementations(fromMethod, toMethod);
}

+ (UINib *)jrsdk_bundleNibWithNibName:(NSString *)name bundle:(nullable NSBundle *)bundleOrNil {
    NSBundle *finalBundel = bundleOrNil;
    if (!bundleOrNil) {
        //NSString * bundlePath = [[NSBundle mainBundle] pathForResource: @"EpubSDK" ofType :@"bundle"];
        
        finalBundel = [NSBundle bundleForClass:NSClassFromString(name)];//[NSBundle bundleWithPath:bundlePath];
    }
    UINib *nib = [UINib jrsdk_bundleNibWithNibName:name bundle:finalBundel];
    if (!nib) {
        nib = [UINib jrsdk_bundleNibWithNibName:name bundle:bundleOrNil];
    }
    return nib;
}

@end
